<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\ShopCatlog
 *
 * @property int $catid
 * @property int $fid
 * @property int $shop_id
 * @property string|null $name
 * @property int $displayorder
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopCatlogItems[] $catlogItems
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShopCatlog[] $children
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Item[] $items
 * @property-read \App\Models\Shop $shop
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopCatlog newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopCatlog newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopCatlog query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopCatlog whereCatid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopCatlog whereDisplayorder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopCatlog whereFid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopCatlog whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopCatlog whereShopId($value)
 * @mixin \Eloquent
 */
class ShopCatlog extends Model
{
    protected $table = 'shop_catlog';
    protected $primaryKey = 'catid';
    protected $fillable = ['catid','fid','shop_id','name','displayorder'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::deleted(function (ShopCatlog $catlog){
            $catlog->catlogItems()->delete();
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'shop_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function catlogItems()
    {
        return $this->hasMany(ShopCatlogItems::class, 'catid', 'catid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(ShopCatlog::class, 'fid', 'catid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     */
    public function items()
    {
        return $this->hasManyThrough(
            Item::class,
            ShopCatlogItems::class,
            'catid',
            'itemid',
            'catid',
            'itemid');
    }
}
